package com.lanou3g.hw0418.q2;

import java.io.*;
import java.util.Random;

/**
 * Created by dllo on 18/4/20.
 */
public class Main {
    // 目标是1
    public static final long TERGET = 1024L * 1024 * 1024;

    public static void main(String[] args) {
        new Thread() {
            @Override
            public void run() {
                creatFile("a", "/Users/dllo/Desktop/作业/Q2");
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                creatFile("b", "/Users/dllo/Desktop/作业/Q2");
            }
        }.start();

    }

    private static void creatFile(String fileName, String filePath) {
        File file = new File(filePath, fileName);
        // 写文件
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buf = new byte[1024];
            Random random = new Random();
            random.nextBytes(buf);
            long sum = 0;
            while (sum < TERGET) {
                bos.write(buf);
                sum += 1024L;
            }
            bos.write(buf);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
